
import axios from "axios";
import NProgress from "nprogress";
import "nprogress/nprogress.css";

const request = axios.create({
  baseURL: "/api",
  timeout: 20000,
});

// 请求拦截器
request.interceptors.request.use(
  (config) => {
    NProgress.start();
    return config;
  },
  () => {}
);

// 响应拦截器
request.interceptors.response.use(
  (response) => {
    NProgress.done();
    if (response.data.code === 200) {
      return response.data.data;
    } else {
      alert(response.data.message);
      return Promise.reject(response.data.message);
    }
  },
  (err) => {
    NProgress.done();
    alert(err);
    return Promise.reject(err);
  }
);

export default request;


